/* eslint-disable */
import {ToastClass} from '@/plugins/Toast/index.js'
import { Dialog,Toast } from 'vant';
import { translate } from "@/utils/helper.js"



const $lang = translate()
/**
 * @description 请求loading 装饰
 * @param {object} option
 */
export const withLoading = (option = {
  message: `${$lang.loading}`
}) => {
  return function (target, name, descriptor) {
    const originFunc = descriptor.value
    descriptor.value = async function (...args) {
      const Toast = new ToastClass(option)
      Toast.show()
      let res
      try {
        res = await originFunc.apply(this, args)
      } catch (e) {
        res = Promise.reject(e)
      }
      Toast.close()
      return res
    }
  }
}

/**
 * @description 增加接口确认弹窗
 */
export const withConfirm = (option = {
  message: `${$lang.confirm}`,
  className: "errSelfDialog",
}) => {
  return function (target, name, descriptor) {
    const originFunc = descriptor.value
    descriptor.value = async function (...args) {
      let res
      const comfirm = await Dialog.confirm({
        // title: `${$lang.tips}`,
        confirmButtonText:$lang.yes,
        cancelButtonText:$lang.no,
        message: option.message,
        className: "errSelfDialog",
      })
      .then(async () => {
        try {
          res = await originFunc.apply(this, args)
        } catch (e) {        
          res = Promise.reject(e)
          // console.warn(e,"这是装饰器的报错",res);
          // Toast.fail($lang.errorNetwork)
          // return res
        }
        return res
      })
      .catch((res) => {
        // console.warn("取消",res);
        return 
      })

      return comfirm
    }
  }
}